﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AustalSystem.NetWork
{
    /// <summary>
    /// winVerification.xaml 的交互逻辑
    /// </summary>
    public partial class winVerification : Window
    {
        public winVerification(List <string>netCode,List <DateTime >netDateTime,DateTime serverTime)
        {
            InitializeComponent();         
            this.netCode = netCode;
            this.netDateTime = netDateTime;
            this.serverDateTime = serverTime;
        }     

        private List<string> netCode = new List<string>();
        private List<DateTime> netDateTime = new List<DateTime>();
        private DateTime serverDateTime = new DateTime();
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
           

            bool b = false;
            string code = this.txtVerification.Text.ToString().Trim();
            for (int i = 0; i < netCode.Count; i++)
            {
                if (code == netCode[i])
                {
                    if (serverDateTime >= netDateTime[i])
                    {
                        MessageBox.Show("您的软件使用时间已到期,请与作者联系!", "提示", MessageBoxButton.OK);
                        b = false;
                        break;
                    }
                    else
                    {
                        MessageBox.Show("验证成功,您可以免费使用程序至 " + netDateTime[i].ToShortDateString().ToString().Trim(), "提示");

                        try
                        {
                        string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\User.txt";
                        System.IO.FileInfo MyFile = new System.IO.FileInfo(filePath);

                        if (MyFile.Exists)
                        {
                            MyFile.Attributes = System.IO.FileAttributes.Normal;
                            MyFile.Delete();
                        }

                        System.IO.FileStream FS = MyFile.Create();
                        MyFile.Attributes = System.IO.FileAttributes.Normal;
                        FS.Close();
                        System.IO.StreamWriter Sw = MyFile.AppendText();
                        Sw.Write(code, Encoding.GetEncoding("GB2312"), false);
                        Sw.Flush();
                        Sw.Close();
                        b = true;
                        }
                        catch 
                        {
                            MessageBox.Show("验证码编译错误，请与作者联系！","提示",MessageBoxButton.OK);
                            b = false;
                        }
                    }
                }
            }
            if (b)
            {
                this.DialogResult = true;
            }
            this.Close();
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
